package cn.edu.cqupt.cyExam.utils;




import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author ZhangXiaohua
 *
 */
public class Utils 
{
	private static Logger LOG = LoggerFactory.getLogger(Utils.class);
	
	public static String nextNO(String maxNO,String parentNO)
	  {
	    if(maxNO != null)
	    {
	      String nextChirdNO = "";
	      int parentLength = parentNO.length();
	      String chirdNO = maxNO.substring(parentLength);
	      int nextChirdNOInt = Integer.parseInt(chirdNO)+1;
	      switch ((nextChirdNOInt+"").length()) 
	      {
	      case 1:
	        nextChirdNO = parentNO+"0"+nextChirdNOInt;
	        break;
	      case 2:
	        nextChirdNO = parentNO+nextChirdNOInt;
	        break;
	      default:
	        break;
	      }
	      return nextChirdNO;
	      
	    }
	    else
	    {
	      return parentNO+"01";
	    }
	    
	  }

	/**
     * 获取String的MD5值
     *
     * @param info 字符串
     * @return 该字符串的MD5值
     */
    public static String getMD5(String info) {
        try
		{
			//获取 MessageDigest 对象，参数为 MD5 字符串，表示这是一个 MD5 算法（其他还有 SHA1 算法等）：
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			//update(byte[])方法，输入原数据
			//类似StringBuilder对象的append()方法，追加模式，属于一个累计更改的过程
			md5.update(info.getBytes("UTF-8"));
			//digest()被调用后,MessageDigest对象就被重置，即不能连续再次调用该方法计算原数据的MD5值。可以手动调用reset()方法重置输入源。
			//digest()返回值16位长度的哈希值，由byte[]承接
			byte[] md5Array = md5.digest();
			//byte[]通常我们会转化为十六进制的32位长度的字符串来使用,本文会介绍三种常用的转换方法
			return bytesToHex(md5Array);
		} catch (NoSuchAlgorithmException e)
		{
			LOG.error("错误！", e);
		} catch (UnsupportedEncodingException e)
		{
			LOG.error("错误！", e);
		}
        return null;
       
    }

  /**
   * 通过java提供的BigInteger 完成byte->HexString
   */
    private static String bytesToHex(byte[] md5Array) 
    {
        BigInteger bigInt = new BigInteger(1, md5Array);
        return bigInt.toString(16);
    }

//    public static void main(String[] args)
//	{
//		String md5Str = getMD5("123456899");
//		System.out.println(md5Str+",length="+md5Str.length());
////    	String basicSQL = "select DISTINCT users_id, teachers_id, users_name, users_gender, teachOrgs_name, users_mobilePhone, users_Email, titleType_name, users_headPortrait, GROUP_CONCAT(roles_name) AS roles_name FROM view2_teacher_full_info where users_id IS NOT NULL AND state_id = 13 GROUP by users_id, teachers_id, users_name, users_gender, teachOrgs_name, users_mobilePhone, users_Email, titleType_name, users_headPortrait order BY users_id";
//    	
////    	int orderIndex = basicSQL.toLowerCase().indexOf("order by");
////    	String orderBySubStr = (orderIndex != -1) ? basicSQL.substring(orderIndex):"";
////    	String selectFromWhereGroupByStr = (orderIndex != -1) ? basicSQL.substring(0, orderIndex):basicSQL;
////    	
////    	int groupIndex = selectFromWhereGroupByStr.toLowerCase().indexOf("group by");
////    	String groupBySubStr = (groupIndex != -1) ? selectFromWhereGroupByStr.substring(groupIndex):"";
////    	String selectFromWhereStr = (groupIndex != -1) ? selectFromWhereGroupByStr.substring(0, groupIndex):selectFromWhereGroupByStr;
////    	
////    	int whereIndex = selectFromWhereStr.toLowerCase().indexOf("where ");
////    	String whereSubStr = (whereIndex != -1) ? selectFromWhereStr.substring(whereIndex):"";
////    	String selectAndFromBaseStr = (whereIndex != -1) ? selectFromWhereStr.substring(0, whereIndex):selectFromWhereStr;
////    	
////    	
////    	System.out.println("selectAndFromBaseStr="+selectAndFromBaseStr);
////    	System.out.println("whereSubStr="+whereSubStr);
////    	System.out.println("groupBySubStr="+groupBySubStr);
////    	System.out.println("orderBySubStr="+orderBySubStr);
////    	System.out.println("sql="+selectAndFromBaseStr+whereSubStr+groupBySubStr+orderBySubStr);
//    	
//	}

}
